
import axios from "axios";

/**
 * 实例化读取操作的axios对象
 * 用于处理数据读取相关的API请求
 */
const readB = axios.create({
  // 基础路径，所有请求都会基于此路径
  baseURL: "http://47.95.68.41:4550/",

  // 响应超时时间(单位毫秒)
  timeout: 5000
});

/**
 * 请求拦截器 - 读取操作
 * 在请求发送前对请求进行处理
 */
readB.interceptors.request.use(
  config => {
    // 获取token并添加到请求头
    const token = window.localStorage.getItem('token');
    if (token) {
      config.headers['Authorization'] = `Bearer ${token}`;
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
);

/**
 * 响应拦截器 - 读取操作
 * 在收到响应后对响应进行处理
 */
readB.interceptors.response.use(
  res => {
    return res
  },
  error => {
    // 处理401错误（未授权）
    if (error.response && error.response.status === 401) {
      console.log('未授权，请重新登录');
      // 可以在这里处理跳转到登录页
      // window.location.href = '/login';
    }
    return Promise.reject(error)
  }
);

/**
 * 实例化写入操作的axios对象
 * 用于处理数据写入相关的API请求
 */
const writeB = axios.create({
  // 基础路径，所有请求都会基于此路径
  baseURL: "http://47.95.68.41:4560/",

  // 响应超时时间(单位毫秒)
  timeout: 5000
});

/**
 * 请求拦截器 - 写入操作
 * 在请求发送前对请求进行处理
 */
writeB.interceptors.request.use(
  config => {
    // 获取token并添加到请求头
    const token = window.localStorage.getItem('token');
    if (token) {
      config.headers['Authorization'] = `Bearer ${token}`;
    }
    return config
  },
  error => {
    return Promise.reject(error)
  }
);

/**
 * 响应拦截器 - 写入操作
 * 在收到响应后对响应进行处理
 */
writeB.interceptors.response.use(
  res => {
    return res
  },
  error => {
    // 处理401错误（未授权）
    if (error.response && error.response.status === 401) {
      console.log('未授权，请重新登录');
      // 可以在这里处理跳转到登录页
      // window.location.href = '/login';
    }
    return Promise.reject(error)
  }
);



// 导出封装的axios实例，供其他模块使用
export { readB, writeB }

// 疾病科普相关API接口
export interface PopularizationData {
  popularScienceTitle: string;
  popularScienceImages: string;
  diseaseType: string;
  popularScienceStatus: boolean;
  isRecommended: boolean;
  detailedContent: string;
}

export interface PopularizationResponse {
  success: boolean;
  message: string;
  data?: any;
}

// 新增疾病科普
export const addPopularization = (data: PopularizationData): Promise<PopularizationResponse> => {
  return writeB.post('/api/Popularization/AddPopularization/add', data);
};

// 获取疾病科普列表
export const getPopularizationList = (params: {
  PageIndex?: number;
  PageSize?: number;
  PopularScienceTitle?: string;
  DiseaseType?: string;
  StartTime?: string;
  EndTime?: string;
}): Promise<PopularizationResponse> => {
  return readB.get('/api/Popularization/GetPopularizationList/list', { params });
};

// 更新疾病科普
export const updatePopularization = (id: number | string, data: PopularizationData): Promise<PopularizationResponse> => {
  return writeB.put('/api/Popularization/UpdatePopularization/update', { id, ...data });
};

// 逻辑删除疾病科普（单个删除）
export const deletePopularization = (id: number): Promise<PopularizationResponse> => {
  return writeB.post('/api/Popularization/BatchDeletePopularization/batch-delete', { popularizationIds: [id] });
};

// 逻辑删除疾病科普（批量删除）
export const batchDeletePopularization = (ids: number[]): Promise<PopularizationResponse> => {
  return writeB.post('/api/Popularization/BatchDeletePopularization/batch-delete', { popularizationIds: ids });
};

// 获取疾病科普详情
export const getPopularizationDetail = (id: string): Promise<PopularizationResponse> => {
  return readB.get(`/api/Popularization/GetPopularizationDetail/${id}`);
};


